package com.example.zbh.dsliao.model;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;


import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.constant.Constant;
import com.example.zbh.dsliao.model.modelinterface.ModelResgisterInterface;
import com.example.zbh.dsliao.presenter.presenterinterface.PresenterRegisterInterface;

import java.util.Map;

/**
 * Created by zbh on 15/11/16.
 */
public class RegisterModel implements ModelResgisterInterface {

    public RegisterModel() {


    }


    @Override
    public void startCheckUser(Map<String, String> map, final PresenterRegisterInterface presenterRegisterInterface) {
        final String userName = map.get(Constant.USER_NAME);
        final String psw = map.get(Constant.USER_PSW);
        String agaPsw = map.get(Constant.USER_AGA_PSW);

        final Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case Constant.MSG_ONE:
                        presenterRegisterInterface.checkSucResult();
                        break;
                    case Constant.MSG_TWO:
                        presenterRegisterInterface.checkResult("用户名不能为空");
                        break;
                    case Constant.MSG_THREE:
                        presenterRegisterInterface.checkResult("密码不能为空");
                        break;
                    case Constant.MSG_FOUR:
                        presenterRegisterInterface.checkResult("不能为空");
                        break;
                    case Constant.MSG_FIVE:
                        presenterRegisterInterface.checkResult("请输入相同的密码");
                        break;
                    case Constant.MSG_SIX:
                        presenterRegisterInterface.checkFailResult("网络异常,请检查网络");
                        break;
                    case Constant.MSG_SEVEN:
                        presenterRegisterInterface.checkFailResult("用户已存在");
                        break;
                    case Constant.MSG_EIGHT:
                        presenterRegisterInterface.checkFailResult("注册失败无权限");
                        break;
                    case Constant.MSG_NINE:
                        presenterRegisterInterface.checkFailResult("用户名不合法");
                        break;
                    case Constant.MSG_TEN:
                        presenterRegisterInterface.checkFailResult("注册失败");
                        break;


                }

                return false;
            }
        });

        if (TextUtils.isEmpty(userName)) {
            handler.sendEmptyMessage(Constant.MSG_TWO);

            return;
        }
        if (TextUtils.isEmpty(psw)) {
            handler.sendEmptyMessage(Constant.MSG_THREE);

            return;
        }
        if (TextUtils.isEmpty(agaPsw)) {
            handler.sendEmptyMessage(Constant.MSG_FOUR);

            return;
        } else if (!psw.equals(agaPsw)) {
            handler.sendEmptyMessage(Constant.MSG_FIVE);

        }
        if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(psw)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //注册用户
                        EMChatManager.getInstance().createAccountOnServer(userName, psw);
                        handler.sendEmptyMessage(Constant.MSG_ONE);
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                        int errorCode = e.getErrorCode();
                        if (errorCode == EMError.NONETWORK_ERROR) {
                            handler.sendEmptyMessage(Constant.MSG_SIX);

                        } else if (errorCode == EMError.USER_ALREADY_EXISTS) {
                            handler.sendEmptyMessage(Constant.MSG_SEVEN);

                        } else if (errorCode == EMError.UNAUTHORIZED) {
                            handler.sendEmptyMessage(Constant.MSG_EIGHT);

                        } else if (errorCode == EMError.ILLEGAL_USER_NAME) {
                            handler.sendEmptyMessage(Constant.MSG_NINE);

                        } else {
                            handler.sendEmptyMessage(Constant.MSG_TEN);

                        }

                    }

                }
            }).start();
        }

    }
}
